<?php
/*********************************************************************\
* This File is a part of BH-PANEL (Breizh-Heberg Panel v2.1)
* Copyright (C) 2008-2009 the BH Developpers : Vincent Giersch <mail@vincordi.fr>, Cyprien Laleau <fanning.fr@gmail.com> and Edwin Cabiten <marmottes44@hotmail.fr>
* See file AUTHORS to get more informations

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
/*********************************************************************/

// Types BACKUP : 
//  0 : MySQL
//  1 : Files
if(isset($_SESSION['u_nic'])&&isset($_GET['id'])) {

	if($_GET['backup'] == "mysql") {
		$title = "Backups";
		$tplinc = "panel-hebergement-applications-backups-mysql";
		$tpl->assign(array(
			'iconesleft' => 2,
			'prod' => '0',
			'prod_id' => intval($_GET['id']),
			'etat' => 1
		));
		include('php/lib/panel_inc.php');
		$heberg_current = intval($_GET['id']);
		$infos_heberg = mysql_query("SELECT comptes_heberg.id,
										comptes_heberg.u_nic,
										comptes_heberg.uid_linux,
										comptes_heberg.user_linux,
										comptes_heberg.serveur_mysql,
										comptes_heberg.offre,
										comptes_heberg.etat,
										comptes_heberg.mysql AS c_mysql,
										offre_heberg.bases_mysql AS o_mysql
										FROM comptes_heberg 
										LEFT JOIN offre_heberg
										ON comptes_heberg.offre = offre_heberg.id_offre
										WHERE comptes_heberg.id='".$heberg_current."' AND comptes_heberg.u_nic='".$_SESSION['u_nic']."'")or die(mysql_error());
		if(mysql_num_rows($infos_heberg) == 1) {
		$infos_heberg = mysql_fetch_array($infos_heberg);
			if($infos_heberg['etat'] == 1) {
				if(isset($_POST['database'][0])) {
					$nfile = 'backup_'.date('d-m-y_h-m', time()).'_'.$_SESSION['u_nic'].'_'.sha1(time()).sha1($_SESSION['u_nic']).'.sql';
					foreach($_POST['database'] as $base) {
						deconnexionbdd();connexionbdd();mysql_select_db(DATABASE);
						$infos_base = query("SELECT nom_base FROM bases_mysql WHERE id='".$base."' AND id_heberg='".$infos_heberg['id']."'");
						deconnexionbdd();mysql_connect($server[$infos_heberg['serveur_mysql']]['ns'],"root",$server[$infos_heberg['serveur_mysql']]['pass_mysql']);
						if(mysql_num_rows($infos_base) == 1){
							$infos_base = mysql_fetch_array($infos_base);
							$back_base = new MySQLDump($infos_base['nom_base'],ROOT.'data/backups_mysql/'.$nfile,false,false);
							if($_POST['save_content'] == 'd') {
								$back_base->getDatabaseData(); 
							}
							elseif($_POST['save_content'] == 's') {
								$back_base->getDatabaseStructure(); 
							}
							else {
								$back_base->doDump(); 
							}	
						}
					}
					chmod(ROOT.'data/backups_mysql/'.$nfile, 0777);
					deconnexionbdd();connexionbdd();mysql_select_db(DATABASE);
					if($_POST['type_back'] == "dl") {
						header("Content-Type: text/plain"); 
						header('Content-disposition: attachment; filename="'.$nfile.'";');
						header('Content-Transfer-Encoding: binary');
						header('Pragma: no-cache');
						header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
						header('Expires: 0');
						header('Content-Length: ' . filesize(ROOT.'data/backups_mysql/'.$nfile));
						flush();
						readfile(ROOT.'data/backups_mysql/'.$nfile);
						exit;
					}
					elseif($_POST['type_back'] == 'mail') {
						$bound = '------------'.md5(uniqid(rand()));
						$entete = "From: Breizh Bot <robot@breizh-heberg.net>"."\n"
								.'Reply-To: Breizh Héberg <admin@breizh-heberg.net>'."\n"
								.'MIME-Version: 1.0'."\n"
								.'Content-Type: multipart/mixed;'."\n"
								.' boundary="'.$bound.'"';
						$body = 'This is a multi-part message in MIME format.'."\n"
								.'--'.$bound."\n"
								.'Content-Type: text/plain; charset=ISO-8859-1'."\n"
								.'Content-Transfer-Encoding: 7bit'."\n\n"
								.'La backup MySQL demandée de votre hébergement Breizh Héberg est en pièce jointe.'
								."\n\n"
								.'--'.$bound."\n"
								.'Content-Type: text/plain;'."\n".' name="'.basename($nfile).'"'."\n"
								.'Content-Transfer-Encoding: base64'."\n"
								.'Content-Disposition: attachment;'."\n".' filename="'.basename($nfiler).'"'."\n\n"
								.chunk_split(base64_encode(file_get_contents(ROOT.'data/backups_mysql/'.$nfile)))
								.'--'.$bound.'--'."\n";
						$mail = mysql_fetch_array(query("SELECT u_email FROM users WHERE u_nic ='".$_SESSION['u_nic']."'"));
						mail($mail['u_email'], "Backup MySQL", $body, $entete);
						header("Location:/panel/hebergement-applications.html?id=".intval($_GET['id']));
					}
					else {
						query("INSERT INTO hebergdata_backups(id_heberg, type_backup, parametre) VALUES('".intval($_GET['id'])."', '0', '".$nfile."')");
						header("Location:/panel/hebergement-applications.html?id=".intval($_GET['id']));
					}
				}
				else {
					$parse = 1;	
					$basesmysql = query("SELECT id, nom_base FROM bases_mysql WHERE id_heberg='".$infos_heberg['id']."'");
					While($bmysql = mysql_fetch_array($basesmysql)) {
						$tpl->assign_array('bases_mysql',array(
							'nom_base' => $bmysql['nom_base'],
							'id' => $bmysql['id'],
						));	
					}
				}
			}
		}
	}
	elseif($_GET['backup'] == "files") {
		$title = "Backups de fichiers";
		$tplinc = "panel-hebergement-applications-backups-files";
		$tpl->assign(array(
			'iconesleft' => 2,
			'prod' => '0',
			'prod_id' => intval($_GET['id']),
			'etat' => 1
		));
		include('php/lib/panel_inc.php');
		$heberg_current = intval($_GET['id']);
		$infos_heberg = mysql_query("SELECT comptes_heberg.id,
										comptes_heberg.u_nic,
										comptes_heberg.uid_linux,
										comptes_heberg.user_linux,
										comptes_heberg.serveur_mysql,
										comptes_heberg.offre,
										comptes_heberg.etat,
										comptes_heberg.mysql AS c_mysql,
										offre_heberg.bases_mysql AS o_mysql
										FROM comptes_heberg 
										LEFT JOIN offre_heberg
										ON comptes_heberg.offre = offre_heberg.id_offre
										WHERE comptes_heberg.id='".$heberg_current."' AND comptes_heberg.u_nic='".$_SESSION['u_nic']."'")or die(mysql_error());
		if(mysql_num_rows($infos_heberg) == 1) {
			$infos_heberg = mysql_fetch_array($infos_heberg);
			if($infos_heberg['etat'] == 1) {
				if(isset($_POST['dossier'])) {
					$dossier = preg_replace('#([^a-zA-Z0-9\-\_/])+#i', '', $_POST['dossier']);
					if(!substr($dossier,0,1) == "/") { $dossier = "/".$dossier; }
					query("INSERT INTO hebergdata_backups(id_heberg, type_backup, parametre) VALUES('".intval($_GET['id'])."', '1', '".$dossier."')");
					header("Location:/panel/hebergement-applications.html?id=".intval($_GET['id']));
				}
				else {
					$parse = 1;
				}
			}
		}
	
	}
}
else { header("Location:/site/connexion.html"); }

?>